require "rpbk/version"
require "rpbk/set"
require "rpbk/holder"
require "rpbk/ext/renderer"

module Rpbk
  def Rpbk.setup
    @set ||= Set.new
    @set.append(&Proc.new) if block_given?
    @set
  end
  
  def Rpbk.config
    @set
  end
  
  def Rpbk.drop_configuration
    @set = nil
  end
  
  def Rpbk.find key, controller_name = '', action_name = ''
    @set.find key, controller_name, action_name if @set
  end
  
  def Rpbk.layout controller_name = '', action_name = ''
    @set.find_layout controller_name, action_name if @set
  end
  
  def Rpbk.find_with_context key, context
    find key, get_param_from_context(:controller, context), get_param_from_context(:action, context)
  end
  
  private
    def Rpbk.get_param_from_context param, context
      parameters = (context.request.env["action_dispatch.request.path_parameters"] || {}).merge(
                        context.request.env["action_dispatch.request.parameters"] || {})
      (parameters[param.to_s] || parameters[param.to_sym] || '').to_sym
    end
end
